import { IFormState, IScopeFormState } from "./types.ts";
import { ITableCHandle, ITableColumn } from "@/utils/types/table.ts";
import { computed, reactive } from "vue";
import { FormOption } from "@/components/autoForm/types/componentTypes.ts";
import { IDictRes } from "@/utils/types/TRequest.ts";

export const serchOptions = [
  { label: "角色名称", value: "roleName", type: "input" },
  { label: "权限字符", value: "roleKey", type: "input" },
  { label: "起止时间", value: "create", type: "timeRange" },
];

export const columns: ITableColumn[] = [
  {
    title: "岗位编号",
    dataIndex: "roleId",
    key: "roleId",
  },
  {
    title: "岗位名称",
    dataIndex: "roleName",
    key: "roleName",
  },
  {
    title: "权限字符",
    dataIndex: "roleKey",
    key: "roleKey",
  },
  {
    title: "显示顺序",
    dataIndex: "roleSort",
    key: "roleSort",
  },
  {
    title: "状态",
    dataIndex: "statusName",
    key: "statusName",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    key: "createTime",
  },
  {
    title: "操作",
    dataIndex: "operation",
    key: "operation",
    fixed: "right",
    handle: [
      {
        title: "数据权限",
        hasPermi: ["system:role:edit"],
        normal: true, // 正常显示
        funp: ["handleDataScope", ""], // [操作方法，新增或编辑]
      },
      {
        title: "修改",
        hasPermi: ["system:role:edit"],
        normal: true, // 正常显示
        funp: ["handleAdd", "update"], // [操作方法，新增或编辑]
      },
      {
        title: "删除",
        hasPermi: ["system:role:remove"],
        normal: true, // 正常显示
        funp: ["handleDelete", "/system/role"], // [操作方法，新增或编辑]
      },
    ] as ITableCHandle[],
  },
];

export const addOptions = computed<FormOption<IFormState>[]>(() => {
  const o: FormOption<IFormState>[] = [
    {
      label: "角色名称",
      field: "roleName",
      type: "input",
      rules: [{ required: true, message: "请输入角色名称" }],
    },
    {
      label: "权限字符",
      field: "roleKey",
      type: "input",
      rules: [{ required: true, message: "请输入权限字符" }],
    },
    {
      label: "显示排序",
      field: "roleSort",
      type: "inputNumber",
      rules: [{ required: true, message: "请输入显示排序" }],
    },
    {
      label: "状态",
      field: "status",
      type: "radio",
      props: {
        options: [
          { label: "正常", value: "0" },
          { label: "停用", value: "1" },
        ],
      },
    },
    {
      label: "菜单权限",
      field: "menuIds",
      type: "custom",
      span: 24,
    },
    {
      label: "备注",
      field: "remark",
      type: "input",
      span: 24,
    },
  ];
  return o;
});

export const initFormData: Partial<IFormState> = {
  roleName: "",
  roleKey: "",
  roleSort: null,
  status: "0",
  menuIds: [],
  remark: "",
  roleId: null,
  menuCheckStrictly: true,
  changeMenu: false,
};

export const formState = reactive<IFormState>(
  JSON.parse(JSON.stringify(initFormData)),
);

export const scopeOptions = computed<FormOption<IScopeFormState>[]>(() => {
  const o: FormOption<IScopeFormState>[] = [
    {
      label: "角色名称",
      field: "roleName",
      type: "input",
      props: {
        disabled: true,
      },
    },
    {
      label: "权限字符",
      field: "roleKey",
      type: "input",
      props: {
        disabled: true,
      },
    },
    {
      label: "权限范围",
      field: "dataScope",
      type: "loadSelect",
      props: {
        url: "/common/getEnumList/DataScopeEnum",
        handle: (data: IDictRes) => {
          return {
            value: data.code,
            label: data.message,
          };
        },
      },
    },
  ];
  return o;
});
export const initScopeFormData: Partial<IScopeFormState> = {
  roleName: "",
  roleKey: "",
  roleId: null,
  dataScope: "",
  deptIds: [],
};

export const scopeFormState = reactive<IScopeFormState>(
  JSON.parse(JSON.stringify(initScopeFormData)),
);
